import 'package:flutter/material.dart';
import 'pages/grab_orders_page.dart';
import 'pages/orders_page.dart';
import 'pages/messages_page.dart';
import 'pages/profile_page.dart';
import 'pages/login_page.dart';
import 'pages/splash_page.dart';
import 'routes/custom_route.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 检查登录状态
  final prefs = await SharedPreferences.getInstance();
  final isLoggedIn = prefs.getBool('isLoggedIn') ?? false;
  
  // 移除权限初始化
  // await Permission.storage.request();
  
  runApp(MyApp(isLoggedIn: isLoggedIn));
}

class MyApp extends StatelessWidget {
  final bool isLoggedIn;
  
  const MyApp({
    super.key,
    required this.isLoggedIn,
  });

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '搬家服务',
      theme: ThemeData(
        // 使用定制的颜色方案
        colorScheme: ColorScheme.fromSeed(
          seedColor: const Color(0xFFFF7043), // 使用温暖的橙色作为主色调
          primary: const Color(0xFFFF5722), // 主要颜色
          secondary: const Color(0xFF42A5F5), // 次要颜色使用蓝色，表示专业可靠
          // 自定义其他颜色
          surface: Colors.white,
          background: const Color(0xFFFAFAFA),
          onPrimary: Colors.white,
          // 加深主色调的亮度，使其更突出
          brightness: Brightness.light,
        ),
        // 自定义文字主题
        textTheme: const TextTheme(
          titleLarge: TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.bold,
            color: Color(0xFF424242),
          ),
          bodyLarge: TextStyle(
            fontSize: 16,
            color: Color(0xFF616161),
          ),
        ),
        // 自定义底部导航栏主题
        bottomNavigationBarTheme: BottomNavigationBarThemeData(
          selectedItemColor: const Color(0xFFFF5722), // 选中项的颜色
          unselectedItemColor: Colors.grey[600], // 未选中项的颜色
          selectedLabelStyle: const TextStyle(fontSize: 12),
          unselectedLabelStyle: const TextStyle(fontSize: 12),
        ),
        useMaterial3: true,
        // 添加页面切换动画主题
        pageTransitionsTheme: PageTransitionsTheme(
          builders: {
            TargetPlatform.iOS: CupertinoPageTransitionsBuilder(),
            TargetPlatform.android: CupertinoPageTransitionsBuilder(), // 在Android上也使用iOS风格
          },
        ),
      ),
      // 总是从启动页开始
      home: const SplashPage(),
      routes: {
        '/home': (context) => const MyHomePage(),
        '/login': (context) => const LoginPage(),
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _selectedIndex = 0;

  // 定义底部导航项对应的页面
  final List<Widget> _pages = const [
    GrabOrdersPage(),
    OrdersPage(),
    MessagesPage(),
    ProfilePage(),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      body: _pages[_selectedIndex],
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed, // 固定显示所有item
        currentIndex: _selectedIndex,
        onTap: _onItemTapped,
        items: const [
          BottomNavigationBarItem(
            icon: Icon(Icons.local_shipping),
            label: '抢单',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.assignment),
            label: '订单',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.message),
            label: '消息',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: '我的',
          ),
        ],
      ),
    );
  }
}
